package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dto.OrderSettingDTO;
import com.itheima.service.OrderSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
@Api(tags = "预约设置接口")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * 下载预约设置模板
     *
     * @param response
     * @return
     */
    @GetMapping("/download")
    @ApiOperation("下载模板")
    public void download(HttpServletResponse response) {
        orderSettingService.download(response);
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {

        try {
            orderSettingService.upload(file);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据月份查询预约设置
     *
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    @ApiOperation("根据月份查询预约设置")
    public Result getOrderSettingByMonth(String month) {
        try {
            List<Map<String, Integer>> list = orderSettingService.getOrderSettingByMonth(month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 设置每天可预约人数
     *
     * @param orderSettingDTO
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    @ApiOperation("设置每天可预约人数")
    public Result editNumberByOrderDate(@RequestBody OrderSettingDTO orderSettingDTO) {
        try{
            orderSettingService.editNumberByOrderDate(orderSettingDTO);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
